package org.xg.study.pd.util;

import java.util.*;

/**
 * @author CMG
 * @version 1.0
 * @cLassName TopicRandomUtil
 * @description
 * @date 2021/9/13 13:55
 **/
public class TopicRandomUtil {
    /**
     * @author cmg
     * @description //TODO 乱序Map元素，记录对应答案key
     * @date 2021/9/13 14:26
     * @param option 题目
     * @param key 答案
     * @return org.xg.study.pd.util.Topic 乱序后{A=c., c=a., D=b.}
     **/
    public static Topic random(Map<String, String> option, String key) {
        Set<String> keySet = option.keySet();
        ArrayList<String> keyList = new ArrayList<>(keySet);
        Collections.shuffle(keyList);
        HashMap<String, String> optionNew = new HashMap<>();
        int idx = 0;
        String keyNew = "";
        for (String next : keySet) {
            String randomKey = keyList.get(idx++);
            if (key.equals(next)) {
                keyNew = randomKey;
            }
            optionNew.put(randomKey, option.get(next));
        }
        return new Topic(optionNew, keyNew);
    }
}
